package com.zsc.wxm.ysh.anjo.sms.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zsc.wxm.ysh.anjo.result.CodeEnum;
import com.zsc.wxm.ysh.anjo.service.base.exception.AnjoException;
import com.zsc.wxm.ysh.anjo.sms.model.SmsModel;
import com.zsc.wxm.ysh.anjo.sms.service.SmsService;
import com.zsc.wxm.ysh.anjo.utils.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;

/**
 * 短信接口实现
 * @author playmaker
 * @since 2021-01-13
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Resource
    private SmsModel smsModel;


    @Override
    public void send(String phone,String param) throws UnsupportedEncodingException {
        String accessKeyId = smsModel.getAccesskeyId();
        String accessKeySecret = smsModel.getAccesskeySecret();

        log.info("signName 短信签名： [{}]",new String(smsModel.getSign().getBytes(),"UTF-8"));

        //调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //短信客户端
        DefaultProfile profile = DefaultProfile.getProfile("cn-shenzhen", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-shenzhen",smsModel.getProduct(),smsModel.getDomain());

        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对像
        SendSmsRequest request = new SendSmsRequest();
        request.setSysMethod(MethodType.POST);

        //待发送的手机号
        request.setPhoneNumbers(phone);

        //短信签名
        request.setSignName(smsModel.getSign());

        //短信模板
        request.setTemplateCode(smsModel.getTmplateCode());

        //参数
        request.setTemplateParam(param);

        SendSmsResponse sendSmsResponse;

        try {
            sendSmsResponse = acsClient.getAcsResponse(request);
        } catch (ClientException e) {
            log.error("短信发送失败：[{}]", ExceptionUtil.getMessage(e));
            throw new AnjoException(CodeEnum.SEND_SMS_ERROR);
        }

        log.info("返回的状态码：[{}]",sendSmsResponse.getCode());


        if ("isv.BUSINESS_LIMIT_CONTROL".equals(sendSmsResponse.getCode())){
            log.error("发送短信过于频繁：[{}]","code-" + sendSmsResponse.getCode() + ",message-" + sendSmsResponse.getMessage());
            throw new AnjoException(CodeEnum.SMS_SEND_ERROR_BUSINESS_LIMIT_CONTROL);
        }

        if (!"OK".equals(sendSmsResponse.getCode())){
            log.error("短信发送失败：[{}]", "code-" + sendSmsResponse.getCode() + ",message-" + sendSmsResponse.getMessage());
            throw new AnjoException(CodeEnum.SEND_SMS_ERROR);
        }
    }
}
